import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
Vue.use(Router)

//公共路由界面
const commonRoutes = [
  { path: '/', redirect: '/login' },
  {
    path: '/contain',
    name: 'contain',
    component: resolve => require(['@/components/contain.vue'], resolve),
    redirect: '',
    children:[]
  },
  {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: resolve => require(['@/components/login.vue'], resolve)
  },
  {
      path: '/404',
      name: '404',
      meta: {
        title: '404'
      },
      component: resolve => require(['@/components/404.vue'], resolve)
  }
]

// 本地所有的页面 需要配合后台返回的数据生成页面
export const asyncRoutes = {
  home: {
      path: '/home',
      name: 'home',
      meta: { title: '首页' },
      component: resolve => require(['../view/home/home.vue'], resolve)
  },
  adminHome: {
    path: '/home',
    name: 'home',
    meta: { title: '首页' },
    component: resolve => require(['@/admin/adminHome.vue'], resolve)
  },
  addCase: {
    path: '/addCase',
    name: 'addCase',
    meta: { title: '新增方案' },
    component: resolve => require(['@/view/monitor/addCase.vue'], resolve)
  },
  editCase: {
    path: '/editCase',
    name: 'editCase',
    meta: { title: '编辑方案' },
    component: resolve => require(['@/view/monitor/editCase.vue'], resolve)
  },
  dataDetail: {
    path: '/dataDetail',
    name: 'dataDetail',
    meta: { title: '数据详情页' },
    component: resolve => require(['@/view/monitor/dataDetail.vue'], resolve)
  },
  briefCenter: {
    path: '/briefCenter',
    name: 'briefCenter',
    meta: { title: '简报中心' },
    component: resolve => require(['@/view/brief/briefCenter.vue'], resolve)
  },
  createTemplate: {
    path: '/createTemplate/:id',
    name: 'createTemplate',
    meta: { title: '创建模板' },
    component: resolve => require(['@/view/brief/createTemplate.vue'], resolve)
  },
  briefTemplate: {
    path: '/briefTemplate',
    name: 'briefTemplate',
    meta: { title: '模板列表' },
    component: resolve => require(['@/view/brief/briefTemplate.vue'], resolve)
  },
  sucaiList: {
    path: '/sucaiList',
    name: 'sucaiList',
    meta: { title: '素材列表' },
    component: resolve => require(['@/view/brief/sucaiList.vue'], resolve)
  },
  reportList: {
    path: '/reportList',
    name: 'reportList',
    meta: { title: '简报列表' },
    component: resolve => require(['@/view/brief/reportList.vue'], resolve)
  },
  warnCenter: {
    path: '/warnCenter',
    name: 'warnCenter',
    meta: { title: '预警中心' },
    component: resolve => require(['@/view/warn/warnCenter.vue'], resolve)
  },
  warnBind: {
    path: '/warnBind',
    name: 'warnBind',
    meta: { title: '预警绑定' },
    component: resolve => require(['@/view/warn/warnbind.vue'], resolve)
  },
  sreach: {
    path: '/sreach',
    name: 'sreach',
    meta: { title: '智能搜索' },
    component: resolve => require(['@/view/sreach/smartSreach.vue'], resolve)
  },
  reportCenter: {
    path: '/reportCenter/:id',
    name: 'reportCenter',
    meta: { title: '报告中心' },
    component: resolve => require(['@/view/report/reportLists.vue'], resolve)
  },
  reportLists: {
    path: '/reportLists',
    name: 'reportLists',
    meta: { title: '报告列表' },
    component: resolve => require(['@/view/report/reportCenter.vue'], resolve)
  },
  collect: {
    path: '/collect/:id',
    name: 'collect',
    meta: { title: '我的收藏' },
    component: resolve => require(['@/view/collect/collect.vue'], resolve),
    props: (route) => ({ folerId: parseInt(route.params.id) }),
  },
  person: {
    path: '/person',
    name: 'person',
    meta: { title: '联系人管理' },
    component: resolve => require(['@/view/user/userManage.vue'], resolve)
  },
  systemSet: {
    path: '/systemSet',
    name: 'systemSet',
    meta: { title: '系统设置' },
    component: resolve => require(['@/view/user/systemSet.vue'], resolve)
  },
  custom: {
    path: '/custom',
    name: 'custom',
    meta: { title: '客户管理' },
    component: resolve => require(['@/admin/customManage.vue'], resolve)
  },
  cusInfo: {
    path: '/cusInfo',
    name: 'cusInfo',
    meta: { title: '客户信息' },
    component: resolve => require(['@/admin/customInfo.vue'], resolve)
  },
  addCus: {
    path: '/addCus',
    name: 'addCus',
    meta: { title: '添加客户' },
    component: resolve => require(['@/admin/addCus.vue'], resolve)
  },
  accountList: {
    path: '/accountList',
    name: 'accountList',
    meta: { title: '账号列表' },
    component: resolve => require(['@/admin/accoutList.vue'], resolve)
  },
  accountDetail: {
    path: '/accountDetail',
    name: 'accountDetail',
    meta: { title: '账号列表详情' },
    component: resolve => require(['@/admin/accountDetail.vue'], resolve)
  },
  addAccount: {
    path: '/addAccount',
    name: 'addAccount',
    meta: { title: '账号新增' },
    component: resolve => require(['@/admin/addAccout.vue'], resolve)
  },
  loginLogs: {
    path: '/loginLogs',
    name: 'loginLogs',
    meta: { title: '登录日志' },
    component: resolve => require(['@/admin/loginLogs.vue'], resolve)
  },
  operateLogs: {
    path: '/operateLogs',
    name: 'operateLogs',
    meta: { title: '操作日志' },
    component: resolve => require(['@/admin/operateLogs.vue'], resolve)
  },
  monitor: {
    path: '/monitor/:id',
    name: 'monitor',
    meta: { title: '监测中心' },
    component: resolve => require(['@/view/monitor/monitorCenter.vue'], resolve)
  },
  loginer: {
    path: '/loginer',
    name: 'loginer',
    meta: { title: '个人中心' },
    component: resolve => require(['@/components/loginer.vue'], resolve)
  }
}


const createRouter = () => new Router({
  mode: 'history',
  routes: commonRoutes
})

const router = createRouter()

export function resetRouter() {
  let menuList = [];
  let storeRouters = store.state.routers;
  for (let i = 0; i < storeRouters.length; i++) {
    menuList.push(storeRouters[i].component)
  }
  let currentRoute = router.options.routes;
  menuList.forEach(item => {
    currentRoute[1].children.push(asyncRoutes[item])
  });
  currentRoute.push({
    path: '*',
    component: () => import('@/components/404.vue')
  })
  router.addRoutes(currentRoute)
}

export function clearRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
